Postgresql compatibility for agent model

ms32035 10 年之前
父節點
當前提交
b0d64c4735
共有 2 個文件被更改,包括 16 次插入1 次删除
  1. 3 1
      app/models/agent.rb
  2. 13 0
      lib/rdbms_functions.rb

+ 3 - 1
app/models/agent.rb

@@ -10,6 +10,7 @@ class Agent < ActiveRecord::Base
10 10
   include AssignableTypes
11 11
   include MarkdownClassAttributes
12 12
   include JSONSerializedField
13
+  include RDBMSFunctions
13 14
 
14 15
   markdown_class_attributes :description, :event_description
15 16
 
@@ -127,7 +128,8 @@ class Agent < ActiveRecord::Base
127 128
     if keep_events_for == 0
128 129
       events.update_all :expires_at => nil
129 130
     else
130
-      events.update_all "expires_at = DATE_ADD(`created_at`, INTERVAL #{keep_events_for.to_i} DAY)"
131
+      #events.update_all "expires_at = DATE_ADD(`created_at`, INTERVAL #{keep_events_for.to_i} DAY)"
132
+      events.update_all "expires_at = " + rdbms_date_add("created_at","DAY",keep_events_for.to_i) 
131 133
     end
132 134
   end
133 135
 

+ 13 - 0
lib/rdbms_functions.rb

@@ -0,0 +1,13 @@
1
+module RDBMSFunctions
2
+  def rdbms_date_add(source, unit, amount)
3
+    adapter_type = connection.adapter_name.downcase.to_sym
4
+    case adapter_type
5
+      when :mysql
6
+        "DATE_ADD(`#{source}`, INTERVAL #{unit} #{AMOUNT})"
7
+      when :postgresql    
8
+        "(#{source} + INTERVAL '#{amount} #{unit}')"
9
+      else
10
+        raise NotImplementedError, "Unknown adapter type '#{adapter_type}'"
11
+    end
12
+  end
13
+end